package com.android.launcher3;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * TextView that draws a bubble behind the text. We cannot use a
 * LineBackgroundSpan because we want to make the bubble taller than the text
 * and TextView's clip is too aggressive.
 */
public class BubbleTextView extends TextView {
	private ColorFilter mColorFilter;
	private Drawable mBackground;
	private Drawable mBadgeDrawable;
	private Drawable mCompoundDrawable;
	private FastBitmapDrawable mPressDrawable;
	private Paint mPaint;
	private final RectF mRect;
	private Paint mTextPaint;
	private boolean mBackgroundSizeChanged;
	private float mCornerRadius;
	// private boolean mIsPressed;
	private float mPaddingH;
	private float mPaddingV;
	private int mBadgeRightOffset;
	private int mFontSizeDefault;
	private int mFontSizeSmall;
	private static final String TAG = "BubbleTextView";
	private static final float CORNER_RADIUS = 8.0F;
	private static final float PADDING_H = 5.0F;
	private static final float PADDING_V = 1.0F;

	/**
	 * Creates a new BubbleTextView object.
	 * 
	 * @param paramContext
	 */
	public BubbleTextView(final Context paramContext) {
		super(paramContext);

		RectF localRectF = new RectF();
		this.mRect = localRectF;
		init();
	}

	/**
	 * Creates a new BubbleTextView object.
	 * 
	 * @param paramContext
	 * @param paramAttributeSet
	 */
	public BubbleTextView(final Context paramContext,
			final AttributeSet paramAttributeSet) {
		super(paramContext, paramAttributeSet);

		RectF localRectF = new RectF();
		this.mRect = localRectF;
		init();
	}

	/**
	 * Creates a new BubbleTextView object.
	 * 
	 * @param paramContext
	 * @param paramAttributeSet
	 * @param paramInt
	 *            TODO
	 */
	public BubbleTextView(final Context paramContext,
			final AttributeSet paramAttributeSet, final int paramInt) {
		super(paramContext, paramAttributeSet, paramInt);

		RectF localRectF = new RectF();
		this.mRect = localRectF;
		init();
	}

	public void draw(final Canvas canvas) {
		// Log.d(TAG, "BubbleTextView-draw");
		final Drawable background = mBackground;

		if (background != null) {
			final int scrollX = mScrollX;
			final int scrollY = mScrollY;

			if (mBackgroundSizeChanged) {
				background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
				mBackgroundSizeChanged = false;
			}

			if ((scrollX | scrollY) == 0) {
				background.draw(canvas);
			} else {
				canvas.translate(scrollX, scrollY);
				background.draw(canvas);
				canvas.translate(-scrollX, -scrollY);
			}
		}

		// final Layout layout = getLayout();
		// final RectF rect = mRect;
		// final int left = getCompoundPaddingLeft();
		// final int top = getExtendedPaddingTop();
		//
		// rect.set(left + layout.getLineLeft(0) - mPaddingH,
		// top + layout.getLineTop(0) - mPaddingV,
		// Math.min(left + layout.getLineRight(0) + mPaddingH,
		// mScrollX + mRight - mLeft),
		// top + layout.getLineBottom(0) + mPaddingV);

		// 暂时取掉阴影
		// canvas.drawRoundRect(rect, mCornerRadius, mCornerRadius, mPaint);
		super.draw(canvas);
	}

	protected void drawableStateChanged() {
		super.drawableStateChanged();
		Drawable localDrawable1 = this.mBackground;
		int[] arrayOfInt = getDrawableState();
		int j = 0;
		int k = arrayOfInt.length;

		while (j < k) {
			if (arrayOfInt[j] == android.R.attr.state_pressed) {
				Drawable[] arrayOfDrawable = getCompoundDrawables();

				if ((arrayOfDrawable != null) && (arrayOfDrawable[1] != null)
						&& (this.mPressDrawable == null)) {
					this.mCompoundDrawable = arrayOfDrawable[1];

					Rect localRect = arrayOfDrawable[1].copyBounds();
					Bitmap localBitmap = Bitmap.createBitmap(localRect.width(),
							localRect.height(), Bitmap.Config.ARGB_8888);
					int i1 = localRect.width();
					int i2 = localRect.height();
					arrayOfDrawable[1].setBounds(0, 0, i1, i2);
					arrayOfDrawable[1].draw(new Canvas(localBitmap));
					arrayOfDrawable[1].setBounds(localRect);
					this.mPressDrawable = new FastBitmapDrawable(localBitmap);
					this.mPressDrawable.setColorFilter(this.mColorFilter);
					this.mPressDrawable.setCallback(null);
				}

				if (this.mPressDrawable != null) {
					setCompoundDrawablesWithIntrinsicBounds(null,
							this.mPressDrawable, null, null);
					invalidate();
				}
			} else {
				if ((localDrawable1 != null) && (localDrawable1.isStateful())) {
					if (this.mCompoundDrawable != null) {
						this.mCompoundDrawable.clearColorFilter();
						setCompoundDrawablesWithIntrinsicBounds(null,
								this.mCompoundDrawable, null, null);
					}
				}
			}

			j++;
		}
	}

	@Override
	protected boolean setFrame(final int left, final int top, final int right,
			final int bottom) {
		if (mLeft != left || mRight != right || mTop != top
				|| mBottom != bottom) {
			mBackgroundSizeChanged = true;
		}

		return super.setFrame(left, top, right, bottom);
	}

	@Override
	protected boolean verifyDrawable(final Drawable who) {
		return who == mBackground || super.verifyDrawable(who);
	}

	private void init() {
		setFocusable(true);

		Drawable localDrawable1 = getBackground();
		this.mBackground = localDrawable1;
		setBackgroundDrawable(null);
		this.mBackground.setCallback(this);

		Resources localResources = getResources();
		Paint localPaint1 = new Paint(1);
		this.mPaint = localPaint1;

		Paint localPaint2 = this.mPaint;
		int i = localResources.getColor(R.color.bubble_dark_background);
		localPaint2.setColor(i);

		float f1 = localResources.getDisplayMetrics().density;
		float f2 = 8.0F * f1;
		this.mCornerRadius = f2;

		float f3 = 5.0F * f1;
		this.mPaddingH = f3;

		float f4 = 1.0F * f1;
		this.mPaddingV = f4;

		this.mColorFilter = new PorterDuffColorFilter(2130706432,
				PorterDuff.Mode.SRC_ATOP);

		this.mBadgeDrawable = localResources
				.getDrawable(R.drawable.homescreen_menu_noti_bg);
		this.mFontSizeDefault = localResources
				.getDimensionPixelSize(R.dimen.badge_fontsize_default);
		this.mFontSizeSmall = localResources
				.getDimensionPixelSize(R.dimen.badge_fontsize_small);
		this.mBadgeRightOffset = localResources
				.getDimensionPixelSize(R.dimen.badge_right_offset);
		this.mTextPaint = new Paint();
		this.mTextPaint.setAntiAlias(true);
		this.mTextPaint.setTextSize(this.mFontSizeDefault);
		this.mTextPaint.setColor(-1);
		this.mTextPaint.setTextAlign(Paint.Align.CENTER);
	}
}
